<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="型号" prop="model">
        <!-- <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->

        <el-select v-model="queryParams.model" filterable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-input
          v-model="queryParams.color"
          placeholder="请输入颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="固化方式" prop="curingMethod">
        <el-input
          v-model="queryParams.curingMethod"
          placeholder="请输入固化方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="常规固化时间" prop="curingTime">
        <el-input
          v-model="queryParams.curingTime"
          placeholder="请输入常规固化时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流动性" prop="fluidity">
        <el-input
          v-model="queryParams.fluidity"
          placeholder="请输入流动性"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作时间" prop="operationTime">
        <el-input
          v-model="queryParams.operationTime"
          placeholder="请输入操作时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="胶粘剂强度" prop="adhesiveStrength">
        <el-input
          v-model="queryParams.adhesiveStrength"
          placeholder="请输入胶粘剂强度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基材1材质" prop="substrate1Material">
        <el-input
          v-model="queryParams.substrate1Material"
          placeholder="请输入基材1材质"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基材1厚度" prop="substrate1Thickness">
        <el-input
          v-model="queryParams.substrate1Thickness"
          placeholder="请输入基材1厚度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基材2材质" prop="substrate2Material">
        <el-input
          v-model="queryParams.substrate2Material"
          placeholder="请输入基材2材质"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基材2厚度" prop="substrate2Thickness">
        <el-input
          v-model="queryParams.substrate2Thickness"
          placeholder="请输入基材2厚度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类编号" prop="classifyId">
        <el-input
          v-model="queryParams.classifyId"
          placeholder="请输入分类编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="classifyName">
        <el-input
          v-model="queryParams.classifyName"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:adhesive:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:adhesive:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:adhesive:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          plain
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:adhesive:export']"
          >下载</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-upload2"
          v-hasPermi="['system:adhesive:import']"
          size="mini"
          @click="handleImport"
          >导入</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="adhesiveList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="胶粘剂类型" align="center" prop="type" />
      <el-table-column label="组分" align="center" prop="components" />
      <el-table-column label="颜色" align="center" prop="color" />
      <el-table-column label="流动性" align="center" prop="fluidity" />
      <el-table-column label="固化方式" align="center" prop="curingMethod" />
      <el-table-column label="操作时间" align="center" prop="operationTime" />

      <el-table-column label="常温固化时间" align="center" prop="curingTime" />
      <el-table-column
        label="胶粘剂强度"
        align="center"
        prop="adhesiveStrength"
      />

      <el-table-column
        label="储存条件"
        align="center"
        prop="storageConditions"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- 
      <el-table-column
        label="基材1材质"
        align="center"
        prop="substrate1Material"
      />
      <el-table-column
        label="基材1厚度"
        align="center"
        prop="substrate1Thickness"
      />
      <el-table-column
        label="基材2材质"
        align="center"
        prop="substrate2Material"
      />
      <el-table-column
        label="基材2厚度"
        align="center"
        prop="substrate2Thickness"
      /> -->

      <!-- <el-table-column
        label="固化温湿度范围"
        align="center"
        prop="curingConditions"
      />

      <el-table-column label="文件" align="center" prop="fileUrl" />

      <el-table-column label="分类编号" align="center" prop="classifyId" />
      <el-table-column label="分类名称" align="center" prop="classifyName" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:adhesive:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:adhesive:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改胶粘剂信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入型号" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="组分" prop="components">
              <el-input v-model="form.components" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="颜色" prop="color">
              <el-input v-model="form.color" placeholder="请输入颜色" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="固化方式" prop="curingMethod">
              <el-input
                v-model="form.curingMethod"
                placeholder="请输入固化方式"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="常温固化时间" prop="curingTime">
              <el-input
                v-model="form.curingTime"
                placeholder="请输入常温固化时间"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="固化温湿度范围" prop="curingConditions">
              <el-input
                v-model="form.curingConditions"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="流动性" prop="fluidity">
              <el-input v-model="form.fluidity" placeholder="请输入流动性" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间" prop="operationTime">
              <el-input
                v-model="form.operationTime"
                placeholder="请输入操作时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="胶粘剂强度" prop="adhesiveStrength">
              <el-input
                v-model="form.adhesiveStrength"
                placeholder="请输入胶粘剂强度"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="文件" prop="fileUrl">
              <el-input
                v-model="form.fileUrl"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col> -->

          <el-col :span="12">
            <el-form-item label="储存条件" prop="storageConditions">
              <el-input
                v-model="form.storageConditions"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="基材1材质" prop="substrate1Material">
              <el-input
                v-model="form.substrate1Material"
                placeholder="请输入基材1材质"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基材1厚度" prop="substrate1Thickness">
              <el-input
                v-model="form.substrate1Thickness"
                placeholder="请输入基材1厚度"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基材2材质" prop="substrate2Material">
              <el-input
                v-model="form.substrate2Material"
                placeholder="请输入基材2材质"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基材2厚度" prop="substrate2Thickness">
              <el-input
                v-model="form.substrate2Thickness"
                placeholder="请输入基材2厚度"
              />
            </el-form-item>
          </el-col> -->

          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox
              v-model="upload.updateSupport"
            />是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdhesive,
  getAdhesive,
  delAdhesive,
  addAdhesive,
  updateAdhesive,
  getModel,
} from "@/api/glue/adhesive";
import { getToken } from "@/utils/auth";

export default {
  name: "Adhesive",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 胶粘剂信息表格数据
      adhesiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        model: null,
        type: null,
        components: null,
        color: null,
        curingMethod: null,
        curingTime: null,
        curingConditions: null,
        fluidity: null,
        operationTime: null,
        adhesiveStrength: null,
        fileUrl: null,
        storageConditions: null,
        substrate1Material: null,
        substrate1Thickness: null,
        substrate2Material: null,
        substrate2Thickness: null,
        classifyId: null,
        classifyName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/adhesive/import",
      },
      options: [],
    };
  },
  created() {
    this.getList();
    this.getTypeList();
  },
  methods: {
    /** 查询胶粘剂信息列表 */
    getList() {
      this.loading = true;
      listAdhesive(this.queryParams).then((response) => {
        this.adhesiveList = response.rows;
        this.total = response.total;
        this.getTypeList();

        this.loading = false;
      });
    },
    getTypeList() {
      getModel().then((response) => {
        this.options = [];
        response.data.forEach((element) => {
          this.options.push({
            label: element,
            value: element,
          });
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        model: null,
        type: null,
        components: null,
        color: null,
        curingMethod: null,
        curingTime: null,
        curingConditions: null,
        fluidity: null,
        operationTime: null,
        adhesiveStrength: null,
        fileUrl: null,
        remark: null,
        storageConditions: null,
        substrate1Material: null,
        substrate1Thickness: null,
        substrate2Material: null,
        substrate2Thickness: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        classifyId: null,
        classifyName: null,
      };
      this.resetForm("form");
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "胶粘剂信息导入";
      this.upload.open = true;
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },

    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "/system/adhesive/exportTemplate",
        {},
        `胶粘剂导入模板_${new Date().getTime()}.xlsx`
      );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加胶粘剂信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAdhesive(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改胶粘剂信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAdhesive(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdhesive(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除胶粘剂信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAdhesive(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/adhesive/export",
        {},
        `胶粘剂信息列表_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
